package com.rayo.accesscontrollercopy.module.setting.presenter;

import android.content.Context;
import android.os.Message;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BasePresenter;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.biz.BeepBiz;
import com.rayo.accesscontrollercopy.model.biz.IBeepBiz;
import com.rayo.accesscontrollercopy.model.callback.OnGetDbListener;
import com.rayo.accesscontrollercopy.model.callback.OnUpdateDbListener;
import com.rayo.accesscontrollercopy.model.db.bean.ReaderData;
import com.rayo.accesscontrollercopy.module.setting.view.IReaderView;
import com.rayo.wallreader.serialport.OnPortListener;

/**
 * Created by yanghaojie on 16/7/25.
 */
public class ReaderPresenter extends BasePresenter {
    private IReaderView iReaderView;
    private IBeepBiz iBeepBiz;
//    private IReaderBiz iReaderBiz;

    public static final int PLUG_IN_READER = 1;
    public static final int INSET_READER = 2;
    public static final int KEY = 1;
    public static final int CARD = 2;
    public static final int PLUG_IN_CONNECT_BEEP = 1;
    public static final int PLUG_IN_UPDATE_BEEP = 2;
    public static final int PLUG_IN_EVENT_BEEP = 3;
    public static final int INSET_CONNECT_BEEP = 4;
    public static final int INSET_UPDATE_BEEP = 5;
    public static final int INSET_EVENT_BEEP = 6;

    private static final int INIT = 1;
    private static final int SHOW_SNACK_BAR = 2;
    private static final int SHOW_DIALOG = 3;

    private ReaderData readerData;
    private boolean isUpdate = false;

    /**
     * @param context
     * @param iReaderView
     */
    public ReaderPresenter(Context context, IReaderView iReaderView) {
        this.init(context);
        this.iReaderView = iReaderView;
        this.iBeepBiz = new BeepBiz();
    }

    /**
     * @param msg
     */
    @Override
    protected void message(Message msg) {
        switch (msg.what) {
            case INIT:
                iReaderView.showProgressBar(false);
                ReaderData readerData = (ReaderData) msg.obj;
                if (null != readerData)
                    iReaderView.setBeepType(readerData);
                break;
            case SHOW_SNACK_BAR:
                iReaderView.showSnackBar((String) msg.obj);
                break;
            case SHOW_DIALOG:
                iReaderView.showAlertDialog((String) msg.obj);
                break;
            default:
                break;
        }
    }

    /**
     *
     */
    @Override
    public void initData() {
        iBeepBiz.init(new OnGetDbListener() {
            @Override
            public <T> void onSuccess(T t) {
                if (ReaderData.class.getSimpleName().equals(t.getClass().getSimpleName()))
                    obtainMessage(INIT, t).sendToTarget();
                else
                    obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.data_wrong)).sendToTarget();
            }

            @Override
            public void onFailed(int error) {
                obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.setting_get_failed)).sendToTarget();
            }
        });
    }

    /**
     * @param reader
     * @param device
     */
    public void test(int reader, int device) {
        if (PLUG_IN_READER == reader && KEY == device) {

        } else if (PLUG_IN_READER == reader && CARD == device) {

        } else if (INSET_READER == reader && KEY == device) {

        } else if (INSET_READER == reader && CARD == device) {

        }
    }

    private OnPortListener.OnBeepListener onBeepListener = new OnPortListener.OnBeepListener() {
        @Override
        public void beepSuccess() {
            if (isUpdate) {
                iBeepBiz.save(readerData, new OnUpdateDbListener() {
                    @Override
                    public <T> void onSuccess(String action, T t) {
                        if (ReaderData.class.getSimpleName().equals(t.getClass().getSimpleName()))
                            obtainMessage(SHOW_SNACK_BAR, reference.get().getString(R.string.setting_save_successfully)).sendToTarget();
                        else
                            obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.data_wrong)).sendToTarget();
                    }

                    @Override
                    public void onFailed(String action, String error) {
                        obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.setting_save_failed)).sendToTarget();
                    }
                });
            }
        }

        @Override
        public void beepFailed() {

        }
    };

    /**
     * @param beep
     * @param event
     */
    public void beep(int beep, int event, boolean isUpdate) {
        this.isUpdate = isUpdate;
        this.readerData = GlobalVariables.getInstance().getmReaderData();

        switch (event) {
//            case PLUG_IN_CONNECT_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.PLUG_IN_READER, false, false);
//                readerData.setConnect_beep_1(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
//            case PLUG_IN_UPDATE_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.PLUG_IN_READER, false, false);
//                readerData.setUpdate_beep_1(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
//            case PLUG_IN_EVENT_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.PLUG_IN_READER, false, false);
//                readerData.setEvent_beep_1(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
//            case INSET_CONNECT_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.INSET_READER, false, false);
//                readerData.setConnect_beep_2(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
//            case INSET_UPDATE_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.INSET_READER, false, false);
//                readerData.setUpdate_beep_2(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
//            case INSET_EVENT_BEEP:
//                this.iReaderBiz = new ReaderBiz(ReaderBiz.INSET_READER, false, false);
//                readerData.setEvent_beep_2(beep);
//                this.iReaderBiz.beep(beep, onBeepListener);
//                break;
            default:
                break;
        }
    }
}
